package com.xmtlxx.web.controller.tms.controller;

import cn.hutool.core.bean.BeanUtil;
import com.xmtlxx.tms.domain.bo.*;
import com.xmtlxx.tms.domain.vo.*;
import com.xmtlxx.tms.service.*;
import com.xmtlxx.web.controller.tms.service.IOrderDtlService;
import com.xmtlxx.web.controller.tms.service.IOrderService;
import com.xmtlxx.web.controller.tms.service.IWaybillService;
import com.xmtlxx.common.core.controller.BaseController;
import com.xmtlxx.common.core.domain.R;
import com.xmtlxx.common.core.domain.entity.SysUser;
import com.xmtlxx.common.core.validate.AddGroup;
import com.xmtlxx.common.helper.LoginHelper;
import com.xmtlxx.system.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 货主Controller
 *
 * @author xmtlxx
 * @date 2022-08-15
 */
@Validated
@Api(value = "派车控制器", tags = {"订单派车"})
@RequiredArgsConstructor
@RestController
@RequestMapping("/tms/dispatch")
public class DispatchController extends BaseController {

    private final ILinesService iLinesService;

    private final IScheduleService iScheduleService;

    private final IScheduleDtlService iScheduleDtlService;

    private final ICarService iCarService;

    private final IDriverService iDriverService;

    private final IWaybillService iWaybillService;

    private final IOrderService iOrderService;

    private final ISysUserService iSysUserService;

    private final IOrderDtlService iOrderDtlService;

    /**
     * 暂存派车初始化
     */
    @ApiOperation("暂存派车初始化")
    @GetMapping("/getDispatchOrder/{ids}")
    public R<DispatchVo> getDispatchOrder(@PathVariable List<String> ids){
        DispatchVo vo = new DispatchVo();
        List<OrderVo> ordersItems = new ArrayList<>();
        List<OrderDtlVo> ordersDtlItems = new ArrayList<>();
        if(ids != null && ids.size() >0){
            ordersItems= ids.stream().map(id->{
                OrderVo orderVo = iOrderService.queryById(id);
                OrderDtlBo dtlBo = new OrderDtlBo();
                dtlBo.setOrderId(id);
                List<OrderDtlVo> dtlVoList = iOrderDtlService.queryList(dtlBo);
                ordersDtlItems.addAll(dtlVoList);
                orderVo.setChecked(false);
                return orderVo;
            }).collect(Collectors.toList());
            vo.setOrdersItems(ordersItems);
            vo.setOrderDtlItems(ordersDtlItems);
        }

        return R.ok(vo);
    }

    /**
     * 获取承运商
     */
    @ApiOperation("获取承运商")
    @GetMapping("/getCarrierUser")
    public R<List<SysUser>> getCarrierUser(){
        return R.ok(iSysUserService.selectUserAll("smscys", LoginHelper.getDeptId()));
    }

    /**
     * 获取线路
     */
    @ApiOperation("获取线路")
    @PostMapping("/getLinesByClientId")
    R<List<LinesVo>> getLinesByClientId(@RequestBody DispatchVo vo){
        LinesBo bo = new LinesBo();
        BeanUtil.copyProperties(vo,bo);
        return R.ok(iLinesService.selectLinesByClientId(bo));
    }

    /**
     * 获取车次
     */
    @ApiOperation("获取车次")
    @PostMapping("/getScheduleByLineId")
    R<List<ScheduleVo>> getScheduleByLineId(@RequestBody DispatchVo vo){
        ScheduleBo bo = new ScheduleBo();
        BeanUtil.copyProperties(vo,bo);
        bo.setAfterDay(new Date());
        return R.ok(iScheduleService.queryList(bo));
    }

    /**
     * 获取车次
     */
    @ApiOperation("获取车次绑定司机")
    @PostMapping("/getScheduleDtlByScheduleId")
    R<List<ScheduleDtlVo>> getCarAndDriverByScheduleId(@RequestBody DispatchVo vo){
        ScheduleDtlBo bo = new ScheduleDtlBo();
        BeanUtil.copyProperties(vo,bo);
        return R.ok(iScheduleDtlService.queryList(bo));
    }

    /**
     * 派车
     */
    @ApiOperation("派车安排")
    @PostMapping("/dispatch")
    @Transactional(rollbackFor = Exception.class)
    public R<OrderVo> dispatch(@Validated(AddGroup.class) @RequestBody DispatchVo vo) {
        WaybillBo waybillBo = new WaybillBo();
        waybillBo.setOrderId(vo.getOrderId());
        waybillBo.setDispatchTime(new Date());
        waybillBo.setStatus("0");
        waybillBo.setItems(vo.getDtlItems());

        if(vo.getItems() != null && vo.getItems().size() > 0){
            for (DispatchVo dispatchVo:vo.getItems()){
                waybillBo.setCarId(dispatchVo.getCarId());
                waybillBo.setCarNumber(iCarService.queryById(dispatchVo.getCarId()).getCarNumber());
                waybillBo.setDriverId(dispatchVo.getDriverId());
                waybillBo.setDriverName(iDriverService.queryById(dispatchVo.getDriverId()).getPersonName());
                waybillBo.setVolume(new BigDecimal("0"));
                waybillBo.setWeight(new BigDecimal("0"));
                waybillBo.setScheduleId(dispatchVo.getScheduleId());

                List<WaybillDtlVo> dtls = new ArrayList<>();
                if(dispatchVo.getDtlItems() != null && dispatchVo.getDtlItems().size() > 0){
                    for (WaybillDtlVo dtlVo:dispatchVo.getDtlItems()){
                        WaybillDtlVo dtl = new WaybillDtlVo();
                        BeanUtil.copyProperties(dtlVo,dtl,"nums");
                        dtl.setNums(dtlVo.getShares());
                        dtls.add(dtl);
                    }
                }
                waybillBo.setItems(dtls);
                iWaybillService.insertByBo(waybillBo);

            }
        }
        //修改订单
       OrderVo orderVo = iOrderService.queryById(vo.getOrderId());
       orderVo.setStatus("2");//装车
       OrderBo orderBo = new OrderBo();
       BeanUtil.copyProperties(orderVo,orderBo);
       return R.ok(iOrderService.updateByBo(orderBo));
    }

    /**
     * 派车
     */
    @ApiOperation("重新派车安排")
    @PostMapping("/againDispatch")
    @Transactional(rollbackFor = Exception.class)
    public R<Void> againDispatch(@Validated(AddGroup.class) @RequestBody DispatchVo vo){
        return toAjax(iOrderService.insertDispatch(vo));
    }

}
